package config

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/mitchellh/mapstructure"
	"idle/utils/configUtls"
	"log"
)

// G-怪物表.xlsx
type MonsterConfig struct {
	Sid          int    `json:"Sid"`          // Sid
	Name         string `json:"Name"`         // 名称
	Lv           int    `json:"Lv"`           // 等级
	Exp          int    `json:"exp"`          // 获得经验
	ActiveSkill  string `json:"activeSkill"`  // 主动技能
	PassiveSkill string `json:"passiveSkill"` // 被动技能
	AttributeMap string `json:"AttributeMap"` // 属性
}

var monsterConfigList []*MonsterConfig
var monsterConfigMap map[int]*MonsterConfig

func GetMonsterConfig(Sid int) *MonsterConfig {
	if v, ok := monsterConfigMap[Sid]; ok {
		return v
	}
	return nil
}

func GetAllMonsterConfig() []*MonsterConfig {
	result := make([]*MonsterConfig, 0)
	for _, c := range monsterConfigList {
		result = append(result, c)
	}
	return result
}

func loadMonsterConfig(filePath string) {
	xlsx, err := excelize.OpenFile(filePath)
	if err != nil {
		log.Fatal(err)
	}
	sheetName := xlsx.GetSheetName(1)
	rows := xlsx.GetRows(sheetName)

	monsterConfigMap = make(map[int]*MonsterConfig)
	monsterConfigList = make([]*MonsterConfig, 0)

	for _, row := range rows[4:] {
		data := make(map[string]interface{})
		for j, colCell := range row {
			fieldName := rows[1][j]
			if fieldName != "" {
				data[rows[1][j]] = configUtls.GetValueByType(rows[2][j], colCell)
			}
		}
		var config MonsterConfig
		err := mapstructure.Decode(data, &config)
		if err != nil {
			log.Fatal(err)
			return
		}

		monsterConfigMap[config.Sid] = &config
		monsterConfigList = append(monsterConfigList, &config)
	}
}
